package o2o.web.actions.sys;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.PostmanAgentRelationMemory;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.agent.UserAgentRelationMemory;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.DataDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.postman.model.CityPostman;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.sys.AboutUsInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.system.data.SYS_SETTINGS;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
public class AboutUsAction {

    // 关于我们
    @RequestMapping(method = RequestMethod.POST, value = "/about/us/get")
    @ResponseBody
    public String about_us_get(String token, String city, Double lat, Double lng, Long agentId) {

        List<AboutUsInfo> list = listOfAbout();
        if (ValidateUtil.isAbsList(list)) {
            // 处理为代理的服务电话号
            dealServiceTel(token, city, lng, lat, agentId, list.get(0));
            return AjaxResult.successResult(list.size(), list);
        } else {
            //没有的话插入一行
            AboutUsInfo aboutUsInfo = new AboutUsInfo();
            aboutUsInfo.setCreateTime(DateUtils.getCurrTime());
            try {
                DataDaoSql.getInstance().insertObj(PojoTables.ABOUT_US, aboutUsInfo);
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取失败，请稍后再试");
            }
            //重新获取
            list = listOfAbout();
        }

        if (ValidateUtil.isAbsList(list)) {
//            处理为代理的服务电话号
            dealServiceTel(token, city, lng, lat, agentId, list.get(0));
//            dealServiceTel(token, list.get(0));
            return AjaxResult.successResult(list.size(), list);
        }
        return AjaxResult.successResult(0, null);
    }

    // 关于我们编辑
    @RequestMapping(method = RequestMethod.POST, value = "/about/us/modify")
    @ResponseBody
    public String about_us_modify(String token, AboutUsInfo info) {
        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
//        //验证是否有操作权限
//        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
//            return AjaxResult.errorResult("您没有操作权限");
//        }

        if (ValidateUtil.isNotAbsLong(info.getId())) {
            return AjaxResult.errorResult("请选择要修改的记录");
        }

        StringBuilder sb = new StringBuilder();
        sb.append(" update ").append(PojoTables.ABOUT_US).append(" set appName='").append(info.getAppName()).append("',copyright='").append(info.getCopyright())
                .append("',company='").append(info.getCompany()).append("',isShowCompany=").append(info.getIsShowCompany()).append(",technicalSupport='")
                .append(info.getTechnicalSupport()).append("',isShowTechnicalSupport=").append(info.getIsShowTechnicalSupport()).append(",serviceTel='")
                .append(info.getServiceTel()).append("',note='").append(info.getNote())
                .append("',note1='").append(info.getNote1())
                .append("',note2='").append(info.getNote2())
                .append("',note3='").append(info.getNote3())
                .append("',isShowNote=").append(info.getIsShowNote())
                .append(",techSupportUrl='").append(info.getTechSupportUrl()).append("'")
                .append(",msgImageUrl='").append(info.getMsgImageUrl()).append("'")
                .append(",msgTechnicalSupport='").append(info.getMsgTechnicalSupport()).append("'")
                .append(",msgUrl='").append(info.getMsgUrl()).append("'")
                .append(",isShowMsg=").append(info.getIsShowMsg())
                .append(" where delFlag = 0 and id = ").append(info.getId());
        try {
            DataDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }
        return AjaxResult.successResult();
    }


    //---------------------------
    //获取关于我们
    private List<AboutUsInfo> listOfAbout() {

        StringBuffer sb = new StringBuffer();
        sb.append(" select * from ").append(PojoTables.ABOUT_US).append(" where delFlag = 0");

        List<AboutUsInfo> list = null;
        try {
            list = (List<AboutUsInfo>) DataDaoSql.getInstance().list(AboutUsInfo.class, sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        if (null != list && list.size() > 0) {
            return list;
        }

        return null;
    }

    // 处理代理的电话号
    public static void dealServiceTel(String token, String city, Double lng, Double lat, Long agentId, AboutUsInfo info) {
        if (SYS_SETTINGS.SERVICE_TEL_USE_ADMIN != 1) {
            return;
        }
        // 使用代理的
        if (StringUtils.isEmpty(token) || info == null) {
            return;
        }
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return;
        }
        // dianpu  ?
        String sysCityUsername = null;
        ShopInfo shop = ShopMemory.getInstance().getShopByUsername(user.getUsername());
        if (shop != null) {
            if (SYS_SETTINGS.SHOP_BELONG_MULTIPLE_AGENT == 1 && StringUtils.isNotEmpty(shop.getCityAdmin())) {
                sysCityUsername = shop.getCityAdmin();
            } else {
                agentId = ShopAgentRelationMemory.getInstance().getAgentByShopId(shop.getId());
                List<String> sysCityAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                if (sysCityAdminList != null && sysCityAdminList.size() > 0) {
                    sysCityUsername = sysCityAdminList.get(0);
                }
            }
        }
        // 骑手
        if (StringUtils.isEmpty(sysCityUsername)) {
            CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
            if (cityPostman != null) {
                agentId = PostmanAgentRelationMemory.getInstance().getAgentByPostmanUsername(cityPostman.getUsername());
                List<String> sysCityAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
                if (sysCityAdminList != null && sysCityAdminList.size() > 0) {
                    sysCityUsername = sysCityAdminList.get(0);
                }
            }
        }
        // 用户
//        if (StringUtils.isEmpty(sysCityUsername)) {
//            if (ValidateUtil.isID(agentId)) {
//                List<String> sysCityAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
//                if (sysCityAdminList != null && sysCityAdminList.size() > 0) {
//                    sysCityUsername = sysCityAdminList.get(0);
//                }
//            } else if (ValidateUtil.isNotNull(city) && ValidateUtil.isLatLng(lat, lng)) {
//                List<CityAgentVO> agentList = CityAgentMan.getCityAgentList(city, lat, lng);
//                if (ValidateUtil.isAbsList(agentList)) {
//                    agentId = agentList.get(0).getId();
//                }
//                List<String> sysCityAdminList = UserAgentRelationMemory.getInstance().usernameListOfAgentId(agentId);
//                if (sysCityAdminList != null && sysCityAdminList.size() > 0) {
//                    sysCityUsername = sysCityAdminList.get(0);
//                }
//            }
//        }
        // 修改电话
        if (StringUtils.isNotEmpty(sysCityUsername)) {
            UserInfo sys = UserInfoMemory.getInstance().getUserInfo(sysCityUsername);
            if (sys != null && StringUtils.isNotEmpty(sys.getTelPhone())) {
                info.setServiceTel(sys.getTelPhone());
            }
        }
    }

}
